package com.lebo.portal.controller.base;

import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.LocaleResolver;

import com.lebo.portal.constant.Constants;
import com.lebo.portal.model.LoginUser;
import com.lebo.portal.util.EncryptUtil;

@CrossOrigin
public abstract class BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected LocaleResolver localeResolver;
    private static final String UNKNOWN = "unknown";

    /**
     * 获取登录用户
     * @return 
     */
    @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE)
    public LoginUser getLoginUser(HttpSession session) {
        return (LoginUser) session.getAttribute(Constants.SESSION_ACCOUNT_LABLE);
    }

    @ModelAttribute(Constants.REQUEST_IP)
    public String getRequestIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    @ModelAttribute(Constants.REQUEST_LOCALE)
    protected Locale resolveLocale(HttpServletRequest request) {
        return localeResolver.resolveLocale(request);
    }

    @ModelAttribute(Constants.SESSION_LABLE)
    public String getSessionId(HttpSession session) {
        return session.getId();
    }

    protected String md5Pwd(String userName, String password) throws NoSuchAlgorithmException {
        return EncryptUtil.encryptPassword(StringUtils.lowerCase(StringUtils.trim(userName)), password);
    }

    protected static class Validator {
        public static boolean length4To15(String value) {
            if (null == value) {
                return false;
            }
            if (4 <= value.length() && 15 >= value.length()) {
                return true;
            }
            return false;
        }

        public static boolean length8To16(String value) {
            if (null == value) {
                return false;
            }
            if (8 <= value.length() && 16 >= value.length()) {
                return true;
            }
            return false;
        }

        public static boolean alphaAndNum(String value) {
            if (null == value) {
                return false;
            }
            if (value.matches("^[a-zA-Z]\\w{3,14}$")) {
                return true;
            }
            return false;
        }

        public static boolean numOnly(String value) {
            if (null == value) {
                return false;
            }
            if (value.matches("\\d+")) {
                return true;
            }
            return false;
        }

        public static boolean length8To64(String value) {
            if (null == value) {
                return false;
            }
            if (8 <= value.length() && 64 >= value.length()) {
                return true;
            }
            return false;
        }
    }
}
